package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
         //   添加和修改检查组
        Integer groupId = checkGroup.getId(); //   实体类 存在id  说明修改操作
        if(groupId!=null){
            // update  先删除 中间表数据  清空中间表数据
            baseMapper.relieveByGroupId(groupId);
        }
        saveOrUpdate(checkGroup);  // 如果存在id ---update  如果不存在id  save
        groupId = checkGroup.getId(); //  获取检查组的主键
        if(checkitemIds!=null&&checkitemIds.length!=0){
//            for (Integer checkitemId : checkitemIds) {
//                //  更新中间表   检查组和检查项中间表
//                baseMapper.addCheckGroupAndCheckItemIds(groupId,checkitemId);
//            }
            baseMapper.addAllCheckGroupAndCheckItemIds(groupId,checkitemIds);

        }
    }

    @Override
    public Map findPageByContion(QueryPageBean queryPageBean) {
        // LambdaQueryWrapper 条件构造器
        LambdaQueryWrapper<CheckGroup> lambda = new QueryWrapper<CheckGroup>().lambda();
         String queryString = queryPageBean.getQueryString();//  条件
        queryString=queryString==null?"":queryString;
        //  对象名::方法名
        lambda.eq(CheckGroup::getIs_delete,0);//   默认 and 条件
        String finalQueryString = queryString;
        lambda.and(i->i.like(CheckGroup::getName, finalQueryString).or().like(CheckGroup::getHelpCode, finalQueryString));
//        lambda.like(CheckGroup::getName,queryString).or().like(CheckGroup::getHelpCode,queryString);
        //  select  * from xxx where is_delete=0 and name like  xx  or  helpCode like ""
        //  分页请求参数条件
        Page<CheckGroup> pageResult = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), lambda);
        List<CheckGroup> checkItemList = pageResult.getRecords();
        long total = pageResult.getTotal();
        Map map = new HashMap(); //  定义一个Map对象 封装分页查询结果集数据
        map.put("list",checkItemList);  //  分页数据
        map.put("total",total);  // 总记录数
        return map;
    }

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);//  有效数据
        return list(queryWrapper);
    }
}
